package BDLayer;

import java.sql.*;

public class DBConnection {
	
//	private static final String driver ="jdbc:sqlserver://localhost:1433";
//	private static final String databaseName = ";databaseName=StokoPlastAS"; //insert database name
//	private static String userName ="; user=stokoAdmin";
//	private static String password = ";password=stoko";   //Insert key
	
	
	//Balder UCN DATABASE CONNECTION
	private static final String driver ="jdbc:sqlserver://balder.ucn.dk";
	private static final String databaseName = ";databaseName=dmaa0913_4"; //insert database name
	private static String userName ="; user=dmaa0913_4";
	private static String password = ";password=MaaGodt";   //Insert key
	
	
	private DatabaseMetaData dma;
	private static Connection con;
	private static DBConnection instance = null;
	
	
	private DBConnection()
	{
		String url = driver + databaseName + userName + password;
		
		try{
			//load of driver
			//SQL Server
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			System.out.println("load of class ok");
			
		} //end try
		catch(Exception e){
			System.out.println("Can not find the driver");
			System.out.println(e.getMessage());
			
		}//end catch
		
		
		try{
			//connection to the database
			
			con = DriverManager.getConnection(url);
			con.setAutoCommit(true);
			dma = con.getMetaData(); //get meta data
			
			System.out.println("Connection to " + dma.getURL());
			System.out.println("Driver " + dma.getDriverName());
			System.out.println("Database product name " + dma.getDatabaseProductName());
			
		}//end try
		catch(Exception e){
			
			System.out.println("Problems with the connection to the database");
			System.out.println(e.getMessage());
			System.out.println("url incorrect" + url);
		}//end catch
	}//end constructor
	
	
	//get instance of DBConnection - singleton -
	public static DBConnection getInstance()
	{
		if(instance == null)
		{
			instance = new DBConnection();
		}
		return instance;
	}
	
	//closeDB: closes the connection to the database
	public static void closeConnection()
	{
		try{
			con.close();
			System.out.println("The connection is closed");
		}//end try
		catch(Exception e){
			System.out.println("Error trying to close the database connection");
			
		}//end catch
		
		
	}
	
	//getDBcon: get-metoder, returnerer forbindelsen til databasen
	public Connection getDBcon()
	{
		return con;
	}
	
	   public static void startTransaction()
	    { try{
	        con.setAutoCommit(false);
	        }
	      catch(Exception e){
	        System.out.println("fejl start transaction");
	        System.out.println(e.getMessage());
	      }
	    }
	    public static void commitTransaction()
	    { try{
	        con.setAutoCommit(true);
	        }
	      catch(Exception e){
	        System.out.println("fejl commit transaction");
	        System.out.println(e.getMessage());
	      }
	    }
	    public static void rollbackTransaction()
	    { try{
	        con.rollback();
	        con.setAutoCommit(true);
	        }
	      catch(Exception e){
	        System.out.println("fejl rollback transaction");
	        System.out.println(e.getMessage());
	      }
	    }

}//end DBConnection
